﻿import java.util.Iterator;

public class Shape {
	private java.util.List<Line> lines;
	public void removeAllLines() {
		if (lines != null)
			lines.clear();
	}

	public java.util.List<Line> getLines() {
		if (lines == null)
			lines = new java.util.ArrayList<Line>();
		return lines;
	}
	public void setLines(java.util.List<Line> newLines) {
		removeAllLines();
		for (Iterator<Line> iter = newLines.iterator(); iter.hasNext();)
			addLines((Line) iter.next());
	}

	public void addLines(Line newLine) {
		if (newLine == null)
			return;
		if (this.lines == null)
			this.lines = new java.util.ArrayList<Line>();
		if (!this.lines.contains(newLine))
			this.lines.add(newLine);
	}

	/**
	 * 随机画多边形，startPoint记录下一条线的起点，默认值为null；endPoint记录下一条线的终点，默认值为null。
	 */
	public void drawShape(int edgeNum) {
		int num = 0;
		Point startPoint = null;
		Point endPoint = null;
		while (edgeNum > num) {
			Line line = new Line();
			line.drawLine(startPoint, endPoint);
//			line.drawLine(line.getStartPoint(), line.getEndPoint());
//			line.setStartPoint(line.getEndPoint());
			startPoint = line.getEndPoint();
			if (num == edgeNum - 2) {
				endPoint = getFirstPoint();
			}
			addLines(line);
			--edgeNum;
		}
	}

	/**
	 * 主函数
	 */
	public static void main(String[] args) {
		Shape shape = new Shape();
		int edgeNum = 3;
		shape.drawShape(edgeNum);

		for (Line line : shape.lines) {
			Point spoint = line.getStartPoint();
			Point epoint = line.getEndPoint();
			System.out
					.println("line point:" + spoint.getX() + "-"
							+ spoint.getY() + "、" + epoint.getX() + "-"
							+ epoint.getY());
		}
	}

	/**
	 * 获得画多边形的第一点：lines(0).getStartPoint()
	 */
	public Point getFirstPoint() {
		Point sPoint = new Point();
		sPoint = lines.get(0).getStartPoint();
		return sPoint;
	}
}
